import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Layout from '@/components/layout/index.vue'

const router = createRouter({
  //history: createWebHistory(import.meta.env.BASE_URL),
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Layout',
      component: Layout,
      redirect: '/home',
      children: []
    }
  ]
})

// router.addRoute({
//   path: '/about',
//   name: 'about',
//   component: Layout,
//   redirect: '/about/page',
//   children: [
//     {
//       path: 'page',
//       component: () => import('../views/AboutView.vue')
//     }
//   ]
// })

router.addRoute({
  path: '/home',
  name: 'home',
  component: Layout,
  redirect: '/home/page',
  children: [
    {
      path: 'page',
      component: HomeView
    }
  ]
})

//根据文件生成路由
// const pages = import.meta.glob('@/pages/**/*.vue')
// let routerChildren = <Router.RouterRaw[]>[]
// Object.keys(pages).forEach((key) => {
//   let pathArr: string[] = key.match(/(?<=pages)(.*?)(?=\.vue)/g) || ['']
//   let component: () => Promise<any> = pages[key]
//   routerChildren.push({
//     path: pathArr[0],
//     component: component
//   })
// })
// router.addRoute({
//   path: '/page',
//   component: Layout,
//   children: routerChildren
// })

console.log(router.getRoutes())

router.beforeEach(async (to, from, next) => {
  console.log(to, from, next)
  next()
})

router.afterEach(async () => {})

export default router
